// $(function(){
			// var button = {$button||default='[]'};
			// var temp = eval(button);
			// get_button(temp.button);
			// init();
		// });
		// 全局button变量
var button = [];
function get_button(data){
	if($.isArray(data)&&data.length>0){
		button = data;
	}
}
// 初始化菜单
function init(){
	var menu_len = button.length;
	if(menu_len==0){
		// 一级菜单
		$(".custom-menu-view__menu").each(function(){
			if($(this).attr("alt")==3){
				$(this).show();
				$(this).css({
					width:'100%',
				})
			}
			else{
				$(this).hide();
			}
		});
		$('.cm-edit-before').siblings().hide();
	}
	else if(menu_len==1){
		// 一级菜单
		$(".custom-menu-view__menu").each(function(){
			if($(this).attr("alt")==0||$(this).attr("alt")==3){
				$(this).show();
				$(this).css({
					width:'50%',
				})
			}
			else{
				$(this).hide();
			}
		});
		$(".custom-menu-view__menu[alt='0']>.text-ellipsis").html(button[0]['name']);
		$('.cm-edit-after').hide();
		// 二级菜单
		sub_init(0);
	}
	else if(menu_len==2){
		// 一级菜单
		$(".custom-menu-view__menu").each(function(){
			if($(this).attr("alt")!=2){
				$(this).show();
				$(this).css({
					width:'33.3333%',
				})
			}
			else{
				$(this).hide();
			}
		});
		$(".custom-menu-view__menu[alt='0']>.text-ellipsis").html(button[0]['name']);
		$(".custom-menu-view__menu[alt='1']>.text-ellipsis").html(button[1]['name']);
		$('.cm-edit-after').hide();
		// 二级菜单
		sub_init(0);
		sub_init(1);
	}
	else if(menu_len==3){
		// 一级菜单
		$(".custom-menu-view__menu").each(function(){
			if($(this).attr("alt")!=3){
				$(this).show();
				$(this).css({
					width:'33.3333%',
				})
			}
			else{
				$(this).hide();
			}
		});
		$(".custom-menu-view__menu[alt='0']>.text-ellipsis").html(button[0]['name']);
		$(".custom-menu-view__menu[alt='1']>.text-ellipsis").html(button[1]['name']);
		$(".custom-menu-view__menu[alt='2']>.text-ellipsis").html(button[2]['name']);
		$('.cm-edit-after').hide();
		// 二级菜单
		sub_init(0);
		sub_init(1);
		sub_init(2);
	}
	else{
		alert('menu obj error!');
	}
}

function sub_init(alt='0'){
	if(typeof(button[alt]['sub_button'])!='undefined'){
		if(button[alt]['sub_button'].length<5){
			$(".custom-menu-view__menu[alt='"+alt+"']>ul>li[row='5']").show();
			for(var i = 0;i<button[alt]['sub_button'].length;i++){
				$(".custom-menu-view__menu[alt='"+alt+"']>ul>li[row='"+i+"']").show();
				$(".custom-menu-view__menu[alt='"+alt+"']>ul>li[row='"+i+"']>.text-ellipsis").html(button[alt]['sub_button'][i]['name']);
			}
		}
		else if(button[alt]['sub_button'].length==5){
			$(".custom-menu-view__menu[alt='"+alt+"']>ul>li[row='5']").hide();
			for(var i = 0;i<5;i++){
				$(".custom-menu-view__menu[alt='"+alt+"']>ul>li[row='"+i+"']").show();
				$(".custom-menu-view__menu[alt='"+alt+"']>ul>li[row='"+i+"']>.text-ellipsis").html(button[alt]['sub_button'][i]['name']);
			}
		}
		else if(button[alt]['sub_button'].length<0){
			return 'sub_button length error!';
		}
		else{
			$(".custom-menu-view__menu[alt='"+alt+"']>ul>li[row='5']").show();
		}
	}
	else{
		$(".custom-menu-view__menu[alt='"+alt+"']>ul>li[row='5']").show();
	}
}

// 监听点击菜单事件
// 一级菜单
$(".custom-menu-view__menu>.text-ellipsis").on('click',function(){
	$('.cm-edit-before').hide();
	$('.cm-edit-after').show();
	$(".custom-menu-view__menu").removeClass('subbutton__actived');
	$(".custom-menu-view__menu__sub__add").removeClass('subbutton__actived');
	$(this).parent().addClass('subbutton__actived');
	if($(this).parent().attr('alt')=='3'){
		var add_button = new Object();
		add_button.name="新建菜单";
		add_button.type="click";
		add_button.key="new";
		button.push(add_button);
		init();
	}
	else{
		setRight($(this).parent().attr('alt'));
	}
});
// 二级菜单
$(".custom-menu-view__menu__sub__add").on('click',function(){
	$('.cm-edit-before').hide();
	$('.cm-edit-after').show();
	$(".custom-menu-view__menu").removeClass('subbutton__actived');
	$(".custom-menu-view__menu__sub__add").removeClass('subbutton__actived');
	$(this).addClass('subbutton__actived');
	if($(this).attr('row')=='5'){
		var alt = $(this).parent().parent().attr('alt');
		var add_button = new Object();
		add_button['name']="新建子菜单";
		add_button['type']="click";
		add_button['key']="new";
		if(typeof(button[alt].sub_button)=='undefined'||button[alt].sub_button.length<1){
			$('#reminderModal').modal('show');
		}
		else{
			button[alt].sub_button.push(add_button);
			init();
		}
	}
	else{
		setRight($(this).parent().parent().attr('alt'),$(this).attr('row'));
	}
});
// 确定添加二级菜单
$('.reminder').click(function(){
	var alt = $('.subbutton__actived').parent().parent().attr('alt');
	var add_button = new Object();
	add_button['name']="新建子菜单";
	add_button['type']="click";
	add_button['key']="new";
	button[alt].sub_button = [];
	button[alt].sub_button.push(add_button);
	$('#reminderModal').modal('hide');
	init();
});
// 设置右侧信息
function setRight(alt = '0',row = '-1'){
	if(row == '-1'){
		if(typeof(button[alt]['sub_button'])=='undefined'||button[alt]['sub_button'].length<=0){
			if(button[alt]['type']=='view'){
				$('input[type="radio"]').prop('checked',false);
				$('input[value="link"]').prop('checked','checked');
				$('input[name="url"]').val(button[alt]['url']);
				$('#editMsg').hide();
				$('#editPage').show();
			}
			else if(button[alt]['type']=='click'){
				$('input[type="radio"]').prop('checked',false);
				$('input[value="sendmsg"]').prop('checked','checked');
				$('#editMsg').show();
				$('#editPage').hide();
				$('input[name="url"]').val('');
			}
			else{
				$('input[type="radio"]').prop('checked',false);
				$('input[value="sendmsg"]').prop('checked','checked');
				$('#editMsg').show();
				$('#editPage').hide();
				$('input[name="url"]').val('');
			}
			$('#radioGroup').show();
			$("#cm-tit").html(button[alt]['name']);
			$("input[name='custom_input_title']").val(button[alt]['name']);
		}
		else{
			$('#editMsg').hide();
			$('#editPage').hide();
			$('#radioGroup').hide();
			$("#cm-tit").html(button[alt]['name']);
			$("input[name='custom_input_title']").val(button[alt]['name']);
		}
	}
	else if(row>=0){
		if(button[alt]['sub_button'][row]['type']=='view'){
			$('input[type="radio"]').prop('checked',false);
			$('input[value="link"]').prop('checked','checked');
			$('input[name="url"]').val(button[alt]['sub_button'][row]['url']);
			$('#editMsg').hide();
			$('#editPage').show();
		}
		else if(button[alt]['sub_button'][row]['type']=='click'){
			$('input[type="radio"]').prop('checked',false);
			$('input[value="sendmsg"]').prop('checked','checked');
			$('#editMsg').show();
			$('#editPage').hide();
			$('input[name="url"]').val('');
		}
		else{
			$('input[type="radio"]').prop('checked',false);
			$('input[value="sendmsg"]').prop('checked','checked');
			$('#editMsg').show();
			$('#editPage').hide();
			$('input[name="url"]').val('');
		}
		$('#radioGroup').show();
		$("#cm-tit").html(button[alt]['sub_button'][row]['name']);
		$("input[name='custom_input_title']").val(button[alt]['sub_button'][row]['name']);
	}
	else{
		return 'sub_button row error';
	}
}

// radio 点击事件
$('input[name="radioBtn"]').click(function(){
	if($(this).val()=='sendmsg'){
		$('#editMsg').show();
		$('#editPage').hide();
		if($('.custom-menu-view__menu__sub__add').hasClass('subbutton__actived')){
			var alt = $('.subbutton__actived').parent().parent().attr('alt');
			var row = $('.subbutton__actived').attr('row');
			button[alt]['sub_button'][row]['type'] = 'click';
			button[alt]['sub_button'][row]['key'] = 'key';
		}else if($('.custom-menu-view__menu').hasClass('subbutton__actived')){
			var alt = $('.subbutton__actived').attr('alt');
			button[alt]['type'] = 'click';
			button[alt]['key'] = 'key';
		}
	}
	if($(this).val()=='link'){
		$('#editMsg').hide();
		$('#editPage').show();
		var data = $('input[name="url"]').val();
		if($('.custom-menu-view__menu__sub__add').hasClass('subbutton__actived')){
			var alt = $('.subbutton__actived').parent().parent().attr('alt');
			var row = $('.subbutton__actived').attr('row');
			button[alt]['sub_button'][row]['type'] = 'view';
			button[alt]['sub_button'][row]['url'] = data;
		}else if($('.custom-menu-view__menu').hasClass('subbutton__actived')){
			var alt = $('.subbutton__actived').attr('alt');
			button[alt]['type'] = 'view';
			button[alt]['url'] = data;
		}
	}
});

$('input[name="url"]').blur(function(){
	var data = $('input[name="url"]').val();
	if($('.custom-menu-view__menu__sub__add').hasClass('subbutton__actived')){
		var alt = $('.subbutton__actived').parent().parent().attr('alt');
		var row = $('.subbutton__actived').attr('row');
		button[alt]['sub_button'][row]['type'] = 'view';
		button[alt]['sub_button'][row]['url'] = data;
	}else if($('.custom-menu-view__menu').hasClass('subbutton__actived')){
		var alt = $('.subbutton__actived').attr('alt');
		button[alt]['type'] = 'view';
		button[alt]['url'] = data;
	}
});
//删除菜单事件delMenu
$('#delMenu').click(function(){
	var is_button=$('.custom-menu-view__menu').hasClass('subbutton__actived');//一级菜单选择项
	var is_sub_button=$('.custom-menu-view__menu__sub__add').hasClass('subbutton__actived');//二级菜单选中项
	if(is_button){
		var alt = $('.subbutton__actived').attr('alt');
		var len = button.length-1;
		for(alt;alt<len;alt++){
			button[alt]=button[Number(alt)+1];
		}
		button.pop();
		for(var i = 0;i<5;i++){
			$('.custom-menu-view__menu__sub__add[row="'+i+'"]').hide();
		}
		init();
	}
	else if(is_sub_button){
		var row = $('.subbutton__actived').attr('row');
		var alt = $('.subbutton__actived').parent().parent().attr('alt');
		var len = button[alt]['sub_button'].length-1;
		for(row;row<len;row++){
			button[alt]['sub_button'][row] = button[alt]['sub_button'][Number(row)+1];
		}
		button[alt]['sub_button'].pop();
		$('.custom-menu-view__menu__sub__add[row="'+len+'"]').hide();
		init();
	}
	else{
		return 'delete menu not found actived button!';
	}
});

//右侧菜单名称写入事件
$('input[name="custom_input_title"]').keyup(function(){
	var data=$(this).val();
	$('.subbutton__actived>.text-ellipsis').html(data);
	$('#cm-tit').html(data);
});

$('input[name="custom_input_title"]').blur(function(){
	var data = $(this).val();
	if($('.custom-menu-view__menu__sub__add').hasClass('subbutton__actived')){
		var alt = $('.subbutton__actived').parent().parent().attr('alt');
		var row = $('.subbutton__actived').attr('row');
		button[alt]['sub_button'][row]['name'] = data;
	}else if($('.custom-menu-view__menu').hasClass('subbutton__actived')){
		var alt = $('.subbutton__actived').attr('alt');
		button[alt]['name'] = data;
	}
});
